package cn.nexd.location.core;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

/**
 * wifi提取器
 * Created by admin on 15/8/30.
 */
class NexdIndoorWifiExtractor {
    //    private String[] wifilist;

    //    public void setWifiList(String[] wifilist) {
    //        this.wifilist = wifilist;
    //    }

    public double[] extractWifi(String[] wifilist, String wifi_input) throws JSONException {

        double[] test = new double[wifilist.length];

        for (int i = 0; i < test.length; i++) {
            test[i] = 0.0;
        }

        JSONObject jsonObject = new JSONObject(wifi_input);
        Iterator<String> keyIter = jsonObject.keys();
        Double sum = 0.0;
        while (keyIter.hasNext()) {
            String keyStr = keyIter.next();
            String value = jsonObject.getString(keyStr);
            //            Log.d("Debug_round1","Wifi Extraction "+keyStr + " with value: "+value);
            int index = _search_in_array(wifilist, keyStr);
            if (index != -1) {
                test[index] = Double.parseDouble(value) + 100.0;
                sum += test[index];
            }
        }
        return test;
    }

    public Double[] extractWifiWithRef(String wifi_input, String[] wifilist) throws JSONException, JSONException {

        Double[] test = new Double[wifilist.length];

        for (int i = 0; i < test.length; i++) {
            test[i] = 0.0;
        }

        JSONObject jsonObject = new JSONObject(wifi_input);
        Iterator<String> keyIter = jsonObject.keys();
        while (keyIter.hasNext()) {
            String keyStr = keyIter.next();
            String value = jsonObject.getString(keyStr);
            int index = _search_in_array(wifilist, keyStr);
            if (index != -1) {
                test[index] = Double.parseDouble(value) + 100.0;
            }
        }
        return test;
    }

    private int _search_in_array(String[] list, String target) {
        int ind = -1;
        for (int i = 0; i < list.length; i++) {
            if (list[i].equals(target)) {
                ind = i;
                return ind;
            }
        }
        return ind;
    }
}
